import Cookies from 'js-cookie';
import Config from './config';
import { decrypted } from './util/ServerLang';

export const MqttConfig = () => {
    const username = Cookies.get('username') || 'null';
    const pass = decrypted(Cookies.get('NCCLOUD_MQTP') || '');
    return {
        URL: Config.mqtturl(),
        OPTIONS: {
            reconnectPeriod: 1000 * 10,
            username: username,
            password: pass,
            clientId: username + '@weibao_web_' + Math.random().toString(16).substr(2, 8)
        }
    };
};

export const MqttThemes = {
    device_status (item) {
        return item.Uuid + '/WHstatus_ExecState';
    },
    device_process_file (item) {
        return item.Uuid + '/WHstatus_programName';
    },
    device_process_rate (item) {
        return item.Uuid + '/WHstatus_HadCompletedPercent';
    },
    device_issue_file_name (item) {
        return item.Uuid + '/WHstatus_DownloadFile';
    },
    device_issue_file_rate (item) {
        return item.Uuid + '/WHstatus_DownloadProgress';
    },
    device_issue_file_status (item) {
        return item.Uuid + '/WHstatus_DownloadStatus';
    }
};

export const MapMqtts = function ({ themekey, themedata, callback }) {
    const mqtt_maps = {};
    themedata.forEach(element => {
        const key = typeof (MqttThemes[themekey]) === 'string' ? MqttThemes[themekey] : MqttThemes[themekey](element);
        const value = callback;
        mqtt_maps[key] = value;
    });

    return mqtt_maps;
};
